<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 靠右的按钮 -->
      <page-tools :show-before="false">
        <template v-slot:after>
          <el-button icon="el-icon-edit" type="primary" size="small" @click="onClickAdd(1,'0')">添加权限</el-button>
        </template>
      </page-tools>
      <!-- 表格 -->
      <el-table ref="table" row-key="id" :data="tableDate" border>
        <el-table-column label="名称">
          <template v-slot="{ row }">
            <i
              v-if="row.pid === '0'"
              style="margin-right:5px;"
              class="el-icon-folder-opened"
              @click="expend(row)"
            />
            <i
              v-else
              style="margin-right:5px;"
              class="el-icon-view"
            />
            <span class="right-name"> {{ row.name }} </span>
          </template>
        </el-table-column>
        <el-table-column label="标识" prop="code" />
        <el-table-column label="描述" prop="description" />
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button v-if="row.type === 1" type="text" @click="onClickAdd(2 ,row.id)">添加</el-button>
            <el-button type="text" @click="onEdit(row.id)">编辑</el-button>
            <el-button type="text" @click="onClickDel(row.id)">删除</el-button>
          </template>
        </el-table-column>

      </el-table>
    </div>
    <!-- 新增权限的对话框 -->
    <el-dialog
      :title="addOrEditForm.id ? '编辑' : '添加'"
      :visible="updatePermissionDialog"
      width="50%"
      @close="onClose"
    >
      <el-form ref="form" :model="addOrEditForm" :rules="rules" label-width="80px">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="addOrEditForm.name" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="addOrEditForm.code" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="addOrEditForm.description" />
        </el-form-item>
        <el-form-item label="权限开启">
          <el-switch
            v-model="addOrEditForm.enVisible"
            active-color="#66b1ff"
            inactive-color="gray"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="updatePermissionDialog = false">取 消</el-button>
        <el-button type="primary" @click="onSave">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getPermissionList, addPermission, getPermissionInFo, updatePermissionInFo, delPermissionInFo } from '@/api/permission.js'
import { transListToTree } from '@/utils'
export default {
  name: 'Permission',
  data() {
    return {
      tableDate: [],
      updatePermissionDialog: false,
      addOrEditForm: {
        name: '',
        code: '',
        description: '',
        enVisible: '0',
        type: 1,
        pid: ''
      },
      rules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '权限描述不能为空', trigger: 'blur' }]
      }
    }
  },

  created() {
    this.loadPermissionList()
  },

  methods: {
    // 获取所有权限
    async loadPermissionList() {
      const res = await getPermissionList()
      // console.log(res)
      this.tableDate = transListToTree('0', res)
    },
    // 点击展开
    expend(row) {
      // 完整写法 !row.isExand ? true : false
      // 控制子菜单是否展开
      // 监听每一行的状态 为 true or false (直接取反即可)
      this.isExand = !row.isExand
      this.$refs.table.toggleRowExpansion(row, row.isExand)
    },
    // 点击确定 发送请求
    async onSave() {
      try {
        await this.$refs.form.validate()
        if (this.addOrEditForm.id) {
          // 编辑
          await updatePermissionInFo(this.addOrEditForm)
          this.$message.success('编辑成功')
        } else {
          await addPermission(this.addOrEditForm)
          this.$message.success('添加权限成功')
        }
        this.updatePermissionDialog = false
        this.addOrEditForm = {
          name: '',
          code: '',
          description: '',
          enVisible: '0',
          type: 1,
          pid: ''
        }
        this.loadPermissionList()
      } catch (err) {
        console.log(err)
      }
    },
    // 点击添加
    onClickAdd(type, pid) {
      this.addOrEditForm.type = type
      this.addOrEditForm.pid = pid
      this.updatePermissionDialog = true
    },
    // 点击编辑
    async onEdit(id) {
      this.updatePermissionDialog = true
      this.addOrEditForm = await getPermissionInFo(id)
    },
    // 点击删除
    async onClickDel(id) {
      console.log('删除')
      try {
        await delPermissionInFo(id)
        this.$message.success('删除成功')
        this.loadPermissionList()
      } catch (err) {
        console.log(err)
      }
    },
    // 修复表单关闭取消的校验和bug
    onClose() {
      this.$refs.form.clearValidate()
      this.updatePermissionDialog = false
      this.addOrEditForm = {
        name: '',
        code: '',
        description: '',
        enVisible: '0',
        type: 1,
        pid: ''
      }
      this.loadPermissionList()
    }
  }
}
</script>

<style scoped lang="scss">
::v-deep .el-table__expand-icon {
  display: none !important;
}
</style>
